/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rie06.openvoip.client.IHM.home;

import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author clement
 */
public class AddContact {

    private static final Logger logger = Logger.getLogger(AddContact.class);
    private InterfaceClient ihm;

    public AddContact(InterfaceClient ihm) {
        this.ihm = ihm;
    }

    public void receiveAddContact(com.rie06.openvoip.client.Contact contact) {
        logger.log(Level.DEBUG, "AddContact - OK");
        String name = contact.getName();

        //Add to list with modify name
        ihm.getClient().addContactToList(contact.getUuid(), name, contact.getIpAddress(), contact.getPort());
    }

    public void addContact() {
        JOptionPane jop = new JOptionPane();

        InetAddress ipAddress = null;
        while (ipAddress == null) {
            String ip = null;
            ip = JOptionPane.showInputDialog(null, "Veuillez entrer l'adresse IP de votre contact", "Ajouter un nouveau contact", JOptionPane.QUESTION_MESSAGE);
            if (ip == null) {
                break;
            } else {
                if (ip.equals("")) {
                    JOptionPane.showMessageDialog(null, "Veuillez entrer une adresse IP", "Erreur", JOptionPane.ERROR_MESSAGE);
                    ipAddress = null;
                } else {
                    try {
                        ipAddress = InetAddress.getByName(ip);
                        //add
                        ihm.getClient().addContact(ipAddress, 1995);
                    } catch (UnknownHostException ex) {
                        JOptionPane.showMessageDialog(null, "IP incorrecte", "Erreur", JOptionPane.ERROR_MESSAGE);
                        ipAddress = null;
                    }
                }
            }
        }
    }
}
